package com.youlu.campus.service.ca.impl;

import com.mongodb.client.result.DeleteResult;
import com.youlu.campus.entity.CaInfoMappping;
import com.youlu.campus.service.ca.CaInfoMapppingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CaInfoMapppingServiceImpl implements CaInfoMapppingService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean create(CaInfoMappping req) {
        req.setId(null);
        req.setCreatedTime(new Date());
        req = mongoTemplate.insert(req);
        if (StringUtils.isNotBlank(req.getId())) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(CaInfoMappping req) {
        return false;
    }

    @Override
    public List<CaInfoMappping> findByParentId(String parentId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("parentId").is(parentId));
        return mongoTemplate.find(query, CaInfoMappping.class);
    }

    @Override
    public boolean delete(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        DeleteResult deleteResult = mongoTemplate.remove(query, CaInfoMappping.class);
        return deleteResult.getDeletedCount() > 0 ? true : false;
    }

    @Override
    public boolean deleteByParentId(String parentId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("parentId").is(parentId));
        DeleteResult deleteResult = mongoTemplate.remove(query, CaInfoMappping.class);
        return deleteResult.getDeletedCount() > 0 ? true : false;
    }

    @Override
    public List<CaInfoMappping> findByCaInfoId(String caInfoId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("caInfoId").is(caInfoId));
        return mongoTemplate.find(query, CaInfoMappping.class);
    }
}
